/*
 * CDiskSpaceMonitor.h
 *
 *  Created on: 2017年2月4日
 *      Author: zhengboyuan
 */

#ifndef CDISKSPACEMONITOR_H_
#define CDISKSPACEMONITOR_H_

#include "DiskSpaceMonitor.h"
#include <deque>
#include "TCriticalSection.h"
#include "TThread.h"
#include "TEvent.h"


class CDiskSpaceMonitor: public DiskSpaceMonitor, public comn::Thread
{
public:
	struct DiskSpace
	{
		std::string dirpath;
		std::string disk;

		int64_t totalSpace;
		int64_t freeSpace;
		double percent;

		int64_t	minFreeSpace;
		double	minPercent;
	};


	typedef std::deque< DiskSpace >		DiskSpaceArray;

	static const int DISKSPACE_POLL_INTERVAL = 5;

public:
	CDiskSpaceMonitor();
	virtual ~CDiskSpaceMonitor();

	virtual void setListener(DiskSpaceMonitorListener* listener);

	virtual int open();

	virtual void close();

	virtual bool isOpen();

	virtual bool add(const std::string& dirpath, int64_t freeSpace);

	virtual bool add(const std::string& dirpath, double freePercent);

	virtual void clear();


	bool getDiskSpace(const std::string& dirpath, DiskSpace& diskspace);

	void poll();

protected:
	virtual int run();
	virtual void doStop();

protected:

	void add(const DiskSpace& diskspace);
	size_t find(const std::string& dirpath);

	bool check(size_t idx, DiskSpace& diskspace);

	void fireCheck(const DiskSpace& diskspace);

protected:
	DiskSpaceMonitorListener*	m_listener;
	DiskSpaceArray	m_disks;
	comn::CriticalSection	m_cs;

	comn::Event	m_event;

};

#endif /* CDISKSPACEMONITOR_H_ */
